package com.zhang.third.day08;


import com.zhang.third.utils.UserBehavior;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.calcite.shaded.com.google.common.base.Charsets;
import org.apache.flink.shaded.guava18.com.google.common.hash.BloomFilter;
import org.apache.flink.shaded.guava18.com.google.common.hash.Funnels;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.sql.Timestamp;

/**
 * @title: 布隆过滤器--计算uv
 * @author: zhang
 * @date: 2022/4/12 09:18
 */
public class Example1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .readTextFile("/Users/apple/IdeaProjects/flink_1.13/src/main/resources/UserBehavior.csv")
                .map(new MapFunction<String, UserBehavior>() {
                    @Override
                    public UserBehavior map(String value) throws Exception {
                        String[] arr = value.split(",");
                        return new UserBehavior(
                                arr[0], arr[1], arr[2], arr[3],
                                Long.parseLong(arr[4]) * 1000L
                        );
                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<UserBehavior>forMonotonousTimestamps()
                                         .withTimestampAssigner(new SerializableTimestampAssigner<UserBehavior>() {
                                             @Override
                                             public long extractTimestamp(UserBehavior element, long recordTimestamp) {
                                                 return element.ts;
                                             }
                                         })
                )
                .filter(r -> r.type.equals("pv"))
                .keyBy(r -> 1)
                .window(TumblingEventTimeWindows.of(Time.hours(1)))
                .aggregate(
                        new AggregateFunction<UserBehavior, Tuple2<BloomFilter<String>, Long>, Long>() {
                            @Override
                            public Tuple2<BloomFilter<String>, Long> createAccumulator() {
                                return Tuple2.of(
                                        BloomFilter.create(
                                                Funnels.stringFunnel(Charsets.UTF_8),  // 去重数据类型
                                                100000,                // 待去重数据规模
                                                0.01                               // 误判率
                                        ),
                                        0L
                                );
                            }

                            @Override
                            public Tuple2<BloomFilter<String>, Long> add(UserBehavior value, Tuple2<BloomFilter<String>, Long> accumulator) {
                                // 数据一定没有来过
                                if (!accumulator.f0.mightContain(value.userId)) {
                                    accumulator.f0.put(value.userId);  // 将对应的位置为1
                                    accumulator.f1 += 1L;              // 统计值+1
                                }
                                return accumulator;
                            }

                            @Override
                            public Long getResult(Tuple2<BloomFilter<String>, Long> accumulator) {
                                return accumulator.f1;
                            }

                            @Override
                            public Tuple2<BloomFilter<String>, Long> merge(Tuple2<BloomFilter<String>, Long> a, Tuple2<BloomFilter<String>, Long> b) {
                                return null;
                            }
                        },
                        new ProcessWindowFunction<Long, String, Integer, TimeWindow>() {
                            @Override
                            public void process(Integer integer, ProcessWindowFunction<Long, String, Integer, TimeWindow>.Context context, Iterable<Long> elements, Collector<String> out) throws Exception {
                                out.collect("窗口：" + new Timestamp(context.window().getStart()) + "~" + "" +
                                        "" + new Timestamp(context.window().getEnd()) + ",中uv是：" +
                                        "" + elements.iterator().next());
                            }
                        }
                )
                .print();

        env.execute();
    }
}
